<template>
       <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="isShow"
        @close="isShow=false"
        width="70%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>

        </template>

        <div class="default-main ba-table-box">
            <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

            <!-- 表格顶部菜单 -->
            <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
            <TableHeader
                :buttons="['refresh','comSearch', 'quickSearch']"
                :quick-search-placeholder="t('Quick search placeholder', { fields: 'token' })"
            >

            <template #default>
                <el-button color="#40485b" class="table-header-operate" type="info" style="margin-left: 12px;" @click="exportHandle">
                    <Icon color="#ffffff" name="el-icon-Download" />
                    <span class="table-header-operate-text">导出</span>
                </el-button>
            </template>
                
        </TableHeader>

            <!-- 表格 -->
            <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
            <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
            <Table ref="tableRef"></Table>
        </div>

        </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, provide, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { baTableApi } from '/@/api/common'
import TableHeader from '/@/components/table/header/index.vue'
import Table from '/@/components/table/index.vue'
import baTableClass from '/@/utils/baTable'
import { ElNotification, ElLoading } from 'element-plus'

let isShow = ref(false);

const ticketId = ref()




import { defineExpose } from 'vue'

function toggle(id: number){
    isShow.value = !isShow.value 
    baTable.table.filter!.search = [
                {
                    'field': 'ticket_id',
                    'val': id,
                    'operator': '=',
                    'render': 'tags',
                }
            ]
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
}

defineExpose({
    toggle
})

defineOptions({
    name: 'haipi/ticketToken',
})

const { t } = useI18n()
const tableRef = ref()

/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/admin/haipi.ticketToken/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('id'), prop: 'id', align: 'center', width: 70, operator: 'RANGE', sortable: 'custom' },
            { label: t('口令'), prop: 'token', align: 'center', operator: false },
            { label: t('状态'), prop: 'status', align: 'center', render: 'tag', operator: 'eq', sortable: false, replaceValue: { '0': t('未使用'), '1': t('已使用') } },
        ],
        dblClickNotEditColumn: [undefined, 'status']
    },
    {
        defaultItems: { status: '1' },
    }
)
provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
 
})


const exportHandle = ()=>{

let params = {}
//带上页面筛选数据

//加载中转圈圈
const loadIndex = ElLoading.service()

//请求url，请求的是当前业务控制器的export方法
baTable.api.postData('export', params).then(res=>{

    //关闭加载中
    loadIndex.close()
    if(res.code == 1 && res.data.full_url){
        //下载文件
        window.open(res.data.full_url)
    }else{
        ElNotification({
            message: '请求错误',
            type: 'error',
        })
    }
})
}
</script>

<style scoped lang="scss"></style>
